package com.yangxd.hospital.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangxd.hospital.entity.Department;
import com.yangxd.hospital.service.DepartmentService;
import com.yangxd.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangxd
 * @since 2021-05-09
 */
@RestController
@RequestMapping("/hospital/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/getDepartments")
    @ApiOperation(value = "获取科室")
    public Result getDepartments(@RequestParam(required = true,defaultValue = "1" )Integer current,
                                 @RequestParam(required = true,defaultValue = "6")Integer size,
                                 @RequestBody Department department) {

        Page<Department> page = new Page<>(current,size);
        QueryWrapper<Department> wrapper = getQueryWrapper(department);
        IPage<Department> departmentIPage = departmentService.findPageDepartment(page,wrapper);
        long total = departmentIPage.getTotal();
        List<Department> departmentList = departmentIPage.getRecords();
        if(null != departmentList && departmentList.size() > 0) {
            return Result.ok().data("total",total).data("departmentList",departmentList);
        } else {
            return Result.error();
        }


    }

    @GetMapping("/deleteDepartment/{departmentId}")
    @ApiOperation(value = "根据departmentId删除科室")
    public Result deleteDepartment(@PathVariable Integer departmentId) {
        try {
            this.departmentService.deleteById(departmentId);
            return Result.ok().message("删除成功");
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }

    }

    @PostMapping("/saveDepartment")
    @ApiOperation(value = "新增科室")
    public Result saveDepartment(@RequestBody Department department) {
        try{
            this.departmentService.saveDepartment(department);
            return Result.ok().message("新增成功");
        }catch (Exception e){
            return Result.error();
        }
    }



    private QueryWrapper<Department> getQueryWrapper(Department department) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        if (department != null) {
            if (!StringUtils.isEmpty(department.getParentId())) {
                queryWrapper.eq("parent_id", department.getParentId());
            }
            if (!StringUtils.isEmpty(department.getDepartmentDirector())) {
                queryWrapper.eq("department_director", department.getDepartmentDirector());
            }
            if (!StringUtils.isEmpty(department.getDepartmentName())) {
                queryWrapper.eq("department_name", department.getDepartmentName());
            }
            queryWrapper.eq("deleted",0);

        }
        return queryWrapper;

    }


}

